<?php
/**
 * 缓存数据业务逻辑类
 * User: allen
 * Date: 2017-01-18
 * Time: 8:25
 */
quanli_require( 'core/memoryCache.php' );
class cacheDataLogic{
    private $memoryCache_Instance = null;
    private $timeout7days = 604800;                                                         //设置7天过期时间
    private $timeout1days = 86400;                                                          //设置1天过期时间
    private $timeout1hour = 3600;                                                           //设置1小时过期时间
    private $cityId = 0;

    function __construct() {
        $this->memoryCache_Instance = new memoryCache ();
    }

    /**
     * 属性设置
     * @param $name
     */
    public function __set($name,$value) {
        switch ($name) {
            case 'cityId':
                $this->cityId = $value;
                break;

            default :
                break;

        }
    }
    /**
     * 读取数据，并放入缓存
     */
    public function reloadCacheData($nameKey){

        $cacheData = null;
        $timeout = $this->timeout1days;

        switch ($nameKey) {
            case 'city' :                                                       //取得城市数据
                $cacheData = $this->getCity();
                $timeout = $this->timeout7days;
                break;
            case 'goodsHostSale' :                                             //取得对应城市的热销商品
                $cacheData = $this->getGoodsHotSale($this->cityId);
                $timeout = $this->timeout1hour;
            default:
                break;
        }
        $this->memoryCache_Instance->set($nameKey ,$cacheData ,$timeout);
        return $cacheData;
    }

    /*-------------------------------------------------分割线-------------------------------------------------------*/
    /*以下内容为业务逻辑区*/
    /*--------------------*/

    /**
     * 取得城市数据
     * @return array|null
     */
    private function getCity() {
        global $G_SHOP;

        //加载区域类
        quanli_include('module/base/system/sysRegion.class.php');

        //实例区域类
        $sysRegion = new sysRegion( $G_SHOP->DBCA );

        //城市级别
        if (in_array(G_CITY_LEVEL, array(1, 2))) {
            $region_level = G_CITY_LEVEL;
        } else {
            $region_level = 2;
        }
        //查询并返回城市
        $arg_array = array(
            'region_level' => $region_level,
            'sortname' => 'sortOrder',
            'sortorder' => 'ASC'
        );
        return $sysRegion->getAreaManageList( $arg_array );
    }

    /**
     * ======================================
     * 商品热销排行
     * ======================================
     */
    private function getGoodsHotSale($city_id){
        global $G_SHOP;
        //实例商品类
        $quanliGoods = new quanliGoods( $G_SHOP->DBCA );
        //实例商品基础信息类
        $goodsBasic = new goodsBasic( $G_SHOP->DBCA );

        $arg_sale_array = array(
            'cityID' => $city_id,
            'useFlg' => 1,
            'sortname' => 'saletotal',
            'sortorder' => 'DESC',
            'rp' => 5
        );

        $goods_sale_array = $quanliGoods->searchList( $arg_sale_array );
        $shop_array['sale'][] = array();

        if ($goods_sale_array) {
            foreach($goods_sale_array as &$goods_sale) {

                //查询三级商品信息
                $arg_array = array(
                    'postID' => $goods_sale['uid']
                );
                $temp_goods_array = $quanliGoods->searchGoodsSaleDetail( $arg_array );

                //查询甜度
                $arg_array = array(
                    'postID' => $goods_sale['postID'],
                    'basicGroupID' => 3
                );
                $goods_basic_array = $goodsBasic->searchBasicRelation( $arg_array );

                $hotSale[] = array(
                    'goodsName' => $goods_sale['goodsName'],
                    'frenchName' => $goods_sale['frenchName'],
                    'salePrice' => $goods_sale['salePrice'],
                    'saletotal' => $goods_sale['saletotal'],
                    'img_link' => $goods_sale['img_link'],
                    'link' => $goods_sale['link'],
                    'spec' => $temp_goods_array['spec'],
                    'postID' => $goods_sale['uid'],
                    'basicName' => $goods_basic_array ? $goods_basic_array[0]['basicName'] : 1
                );
            }
        }
        return $hotSale;
    }

}